home *** CD-ROM | disk | FTP | other *** search
/ PC Media 4 / PC MEDIA CD04.iso / share / prog / pcl4c42 / crc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-22  |  810 b   |  38 lines

  1. #include <stdio.h>
  2. #include "crc.h"
  3.  
  4. #define POLY 0x1021
  5.  
  6. static unsigned short CRCtable[256];
  7. unsigned short CalcTable(unsigned short,unsigned short,unsigned short);
  8.  
  9. /* initialize CRC table */
  10.  
  11. void InitCRC(void)
  12. {int i;
  13.  for(i=0;i<256;i++) CRCtable[i] = CalcTable(i,POLY,0);
  14. }
  15.  
  16. /* calculate CRC table entry */
  17.  
  18. unsigned short CalcTable(
  19.    unsigned short data,
  20.    unsigned short genpoly,
  21.    unsigned short accum)
  22. {static int i;
  23.  data <<= 8;
  24.  for(i=8;i>0;i--)
  25.          {
  26.           if((data^accum) & 0x8000) accum = (accum << 1) ^ genpoly;
  27.           else accum <<= 1;
  28.           data <<= 1;
  29.          }
  30.  return(accum);
  31. }
  32.  
  33. /* compute updated CRC */
  34.  
  35. unsigned short UpdateCRC(unsigned short crc, unsigned char byte)
  36. {
  37.  return( (crc << 8) ^ CRCtable[ (crc >> 8) ^ byte ] );
  38. }